<?php
/**
 * Created by PhpStorm.
 * User: junyv
 * Date: 2017/4/15
 * Time: 17:11
 */

namespace app\home\controller\user;

class Message extends Common
{
    public function _initialize()
    {
        parent::_initialize();
        $data = [
            'house' => 'loupan',
            'second_house' => 'second',
            'rental' => 'chuzu',
            'house_group'  => 'tuangou'
        ];
        $this->assign('model',$data);
    }
    public function index(){
        $map['type']  = 1;
        $map['model'] = 1;
        $map['gid']   = 0;//用户组id，目前为0
        $map['status'] = 1;
        //读取指定用户消息
        $where = " type = 2 and model = 1 and status = 1 and gid = ".$this->userInfo['id'];
        $lists = db('sysmessage')->where($map)->whereOr($where)->order('id desc')->paginate(20);
        $pages = $lists->render();
        $lists = objToArray($lists);
        foreach($lists['data'] as &$v){
            if(db('sysmsg_read')->where(['mid'=>$v['id'],'uid'=>$this->userInfo['id']])->count() == 0){
                $v['read'] = 0;
            }else{
                $v['read'] = 1;
            }
        }
        $this->assign('page',input('param.page/d',1));
        $this->assign('pagesize',20);
        $this->assign('pages',$pages);
        $this->assign('lists',$lists['data']);
        return $this->fetch();
    }
    public function group(){
        $where['model'] = 'house_group';
        $this->getLists($where);
        return $this->fetch();
    }
    public function house(){
        $where['model'] = 'house';
        $this->getLists($where);
        return $this->fetch();
    }
    public function used(){
        $where['model'] = 'second_house';
        $this->getLists($where);
        return $this->fetch();
    }
    public function rental(){
        $where['model'] = 'rental';
        $this->getLists($where);
        return $this->fetch();
    }
    public function detail(){
        $id = input('param.id/d',0);
        $where['id'] = $id;
        $where['user_id'] = $this->userInfo['id'];
        $info = model('baoming')->where($where)->find();
        $this->assign('info',$info);
        return $this->fetch();
    }
    private function getLists($where){
        $where['user_id'] = $this->userInfo['id'];
        $lists = model('baoming')->where($where)->field('id,house_title,type,house_id,memo,model,create_time')->order('create_time desc')->paginate(15);
        $this->assign('lists',$lists);
        $this->assign('pages',$lists->render());
    }
}